package com.pdf.service;

import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
import com.pdf.util.MyHeaderFooter;
import com.pdf.util.PdfUtil;
import com.pdf.util.Watermark;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;

/**
 * @author ytj
 * @date 2021/12/2
 */
@Service
public class PrintService {

    public void print(HttpServletResponse response) {
        try {
            Document document = new Document(PageSize.A4);// 建立一个Document对象
            PdfWriter writer = PdfWriter.getInstance(document,response.getOutputStream());
            writer.setPageEvent(new Watermark("HELLO ITEXTPDF"));// 水印
            writer.setPageEvent(new MyHeaderFooter());// 页眉/页脚
            document.open();//打开
            document.addTitle("PDF-JAVA");// 标题
            document.addAuthor("YTJ");// 作者
            document.addSubject("Subject@iText pdf sample");// 主题
            document.addKeywords("Keywords@iTextpdf");// 关键字
            document.addCreator("YTJ");// 创建者
            new PdfUtil().nameDoublePdf(document);//生成PDF内容
            document.close();//必须要关闭，否则生成的文件打不开
        } catch (Exception e) {
            System.out.println("创建pdf文件出错:"+e.getMessage());
        }
    }
}
